
package model.service
{
    import flash.net.URLRequest;
    import flash.net.URLRequestHeader;
    import flash.net.URLRequestMethod;

    import flash.net.URLVariables;

    import model.vo.UserVO;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.net.ElfinSerivce;

    [Event(name="complete", type="flash.events.Event")]

    /**
     *  Service class send requests to server side and process
     *  result. This Class contain methos for work with user data.
     */
    public class UserService extends ElfinSerivce
    {


        //-------------------------------------------------------------------------
        // Properties
        //-------------------------------------------------------------------------

        //---------------------------------
        // User
        //---------------------------------

        private var _user:UserVO = null;

        public function get user():UserVO
        {
            _user.authorized = lastData.success;
            _user.defaultWallet = lastData.default_wallet;
            _user.email = lastData.email;
            return _user;
        }

        public function set user(value:UserVO):void
        {
            _user = value;
        }


        //-------------------------------------------------------------------------
        // Methods
        //-------------------------------------------------------------------------

        public function restorePassword(user:UserVO):void
        {
            this._user = user;
            var params:Object = {
                "commit": "Send me reset password instructions",
                "user[email]": user.email
            }

            load(Config.USER_PASSWORD, URLRequestMethod.POST, params);
        }


        /**
         * Signin methos sends for authorize user at the server. Event.COMPLETE event
         * dispathed after successful request.
         *
         * @param user
         */
        public function signin(user:UserVO):void
        {
            this._user = user;

            var params:Object = {
                "user[email]": user.email,
                "user[password]": user.password
            };

            load(Config.USER_SIGN_IN, URLRequestMethod.POST, params);
        }


        /**
         * signup method send user registration info on server to join
         * new user. Event.COMPLETE event dispatched after successful request.
         *
         * @param user
         */
        public function signup(user:UserVO):void
        {
            this._user = user;

            var params:Object = {
                "user[email]": user.email,
                "user[password]": user.password,
                "user[password_confirmation]": user.password,
                "user[default_currency_code]": String(user.defaultCurrency).toUpperCase(),
                "user_name": user.login,
                "time_zone_offset": (new Date()).timezoneOffset
            };

            load(Config.USER_SIGN_UP, URLRequestMethod.POST, params);
        }

        public function signout(user:UserVO):void
        {
            this._user = user;
            load(Config.USER_SIGN_OUT, URLRequestMethod.GET);
        }

    }
}
